package com.alibaba.dubbo.demo.provider.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.demo.SysBackuplogInterface;
import com.alibaba.dubbo.demo.bean.PageBean;
import com.alibaba.dubbo.demo.bean.SysBackuplog;

public class SysBackuplogDaoImpl extends BaseDAO  implements SysBackuplogInterface {

	Logger logger = Logger.getLogger(SysBackuplogDaoImpl.class);
	@Override
	public boolean saveBackupLog(SysBackuplog log) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().save(log);
			return true;
		} catch (Exception e) {
			logger.error("新增备份日志！", e);
			System.out.println("新增备份日志异常:"+e.getMessage());
			return false;
		}
	}

	@Override
	public boolean updateBackupLog(SysBackuplog log) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().update(log);
			return true;
		} catch (Exception e) {
			logger.error("更新备份日志！", e);
			System.out.println("更新备份日志异常:"+e.getMessage());
			return false;
		}
	}

	@Override
	public boolean deleteBackupLog(SysBackuplog log, String date) {
		// TODO Auto-generated method stub
		if(log!=null)
		{
			try {
				getHibernateTemplate().delete(log);
				return true;
			} catch (Exception e) {
				logger.error("删除备份日志！", e);
				System.out.println("删除备份日志异常:"+e.getMessage());
				return false;
			}
		}
		else 
		{
			if(StringUtils.isEmpty(date))
				return true;
			String msql = "delete from sys_backuplog where backuptime <'"+date+"' " ;
			try
			{
				excuteBySql(msql);
				return true;
			}
			catch (Exception e) {
				logger.error("执行"+msql+"出错", e);
				return false;
			}
		}
	}

	@Override
	public PageBean queryAllBackupLog(PageBean pageBean, String startdate, String enddate) {
		// TODO Auto-generated method stub
		String msql = "from SysBackuplog where 1=1 ";
		if(!StringUtils.isEmpty(startdate))
			msql +=" and backuptime >= '%"+startdate+"%'";
		
		if(!StringUtils.isEmpty(enddate))
			msql +=" and backuptime <= '%"+enddate+"%'";
		
		String countSql = "select count(*) " + msql;
		return getPageBean(msql, countSql, pageBean);
	}

	@Override
	public List<SysBackuplog> queryAllBackupLogList(String startdate, String enddate) {
		// TODO Auto-generated method stub
		String msql = "from SysBackuplog where 1=1 ";
		
		if(!StringUtils.isEmpty(startdate))
			msql +=" and backuptime >= '%"+startdate+"%'";
		
		if(!StringUtils.isEmpty(enddate))
			msql +=" and backuptime <= '%"+enddate+"%'";
		return queryBean(msql);
	}

	@Override
	public SysBackuplog queryOneBackupLog(int id) {
		// TODO Auto-generated method stub
		try {
			String msql = "from SysBackuplog where mainid="+id;
			List<SysBackuplog> SysBackuplogList = queryBean(msql);
			if(SysBackuplogList.size()>0)
				return SysBackuplogList.get(0);
			else
				return null;
		} catch (Exception e) {
			return null;
		}
	}
}
